package com.hongning.railway.ui.main.cadremanager;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.reqbean.ReqMemberList;
import com.hongning.railway.entity.resbean.ResMemberInfo;
import com.hongning.railway.ui.main.cadremanager.itemviewmodel.CadreGradeItemViewModel;
import com.hongning.railway.util.Apputils;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * Author: zhangjing
 * DATA:2019-08-29 11:11
 * DESC:
 * email:zhangjingworkmol@163.com
 */
public class CadreGradeFragmentViewModel extends BaseViewModel<AppRepository> {
    public ItemBinding<CadreGradeItemViewModel> item = ItemBinding.of(BR.viewModel, R.layout.item_cadre_grade);
    public ObservableList<CadreGradeItemViewModel> viewlist = new ObservableArrayList<>();
    public ObservableField<String> time = new ObservableField<>();
    public ObservableField<Boolean> enable = new ObservableField<>(true);
    public ObservableField<Boolean> submitenable = new ObservableField<>(true);
    private String id;

    public BindingCommand submitCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            ReqMemberList reqMemberList = new ReqMemberList();
            reqMemberList.setTestinfoid(id);
            reqMemberList.setStaffId(Apputils.getStaffid());
            Observable.fromIterable(viewlist)
                    .map(new Function<CadreGradeItemViewModel, ReqMemberList.Data>() {
                        @Override
                        public ReqMemberList.Data apply(CadreGradeItemViewModel cadreGradeItemViewModel) throws Exception {
                            return cadreGradeItemViewModel.getData();
                        }
                    }).toList().flatMapObservable(new Function<List<ReqMemberList.Data>, ObservableSource<ReqMemberList>>() {
                @Override
                public ObservableSource<ReqMemberList> apply(List<ReqMemberList.Data> data) throws Exception {
                    reqMemberList.setUseridscore(data);
                    return Observable.just(reqMemberList);
                }
            }).flatMap(new Function<ReqMemberList, ObservableSource<BaseResponse<String>>>() {
                @Override
                public ObservableSource<BaseResponse<String>> apply(ReqMemberList reqMemberList) throws Exception {
                    return model.submitCadreMember(reqMemberList);
                }
            }).compose(RxUtils.schedulersTransformer()).compose(RxUtils.bindToLifecycle(getLifecycleProvider())).subscribe(new Consumer<BaseResponse<String>>() {
                @Override
                public void accept(BaseResponse<String> stringBaseResponse) throws Exception {
                    startContainerActivity(CadreResultFragment.class.getCanonicalName());
                    finish();
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    throwable.printStackTrace();
                }
            });

        }
    });


    public BindingCommand saveCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            ReqMemberList reqMemberList = new ReqMemberList();
            reqMemberList.setTestinfoid(id);
            reqMemberList.setStaffId(Apputils.getStaffid());
            Observable.fromIterable(viewlist)
                    .map(new Function<CadreGradeItemViewModel, ReqMemberList.Data>() {
                        @Override
                        public ReqMemberList.Data apply(CadreGradeItemViewModel cadreGradeItemViewModel) throws Exception {
                            return cadreGradeItemViewModel.getData();
                        }
                    }).toList().flatMapObservable(new Function<List<ReqMemberList.Data>, ObservableSource<ReqMemberList>>() {
                @Override
                public ObservableSource<ReqMemberList> apply(List<ReqMemberList.Data> data) throws Exception {
                    reqMemberList.setUseridscore(data);
                    return Observable.just(reqMemberList);
                }
            }).flatMap(new Function<ReqMemberList, ObservableSource<BaseResponse<String>>>() {
                @Override
                public ObservableSource<BaseResponse<String>> apply(ReqMemberList reqMemberList) throws Exception {
                    return model.saveCadreMember(reqMemberList);
                }
            }).compose(RxUtils.schedulersTransformer()).compose(RxUtils.bindToLifecycle(getLifecycleProvider())).subscribe(new Consumer<BaseResponse<String>>() {
                @Override
                public void accept(BaseResponse<String> stringBaseResponse) throws Exception {
                    ToastUtils.showShort("保存成功");
                    enable.set(false);
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    throwable.printStackTrace();
                }
            });

        }
    });

    public CadreGradeFragmentViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }

    public void loadData(String id) {
        this.id = id;
        ReqMemberList reqMemberList = new ReqMemberList();
        reqMemberList.setStaffId(Apputils.getStaffid());
        reqMemberList.setTestinfoid(id);
        model.getCadreMemberList(reqMemberList)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .subscribe(new ApiDisposableObserver<List<ResMemberInfo>>() {
                    @Override
                    public void onResult(List<ResMemberInfo> resMemberInfos) {
                        for (ResMemberInfo data : resMemberInfos) {
                            viewlist.add(new CadreGradeItemViewModel(CadreGradeFragmentViewModel.this, data));
                        }
                    }
                });
    }
}
